﻿using System.Drawing;
using System.Reflection;
using System.Windows.Forms;
using ESRI.ArcGIS.ADF.BaseClasses;
using ESRI.ArcGIS.Controls;
using PlanningGIS.Framework;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.ADF;

namespace PlanningGIS.SelectQuery
{
    /// <summary>
    /// 作用：全图检索Command类
    ///  </summary>
    public class QueryAllLayerButton : BaseCommand
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public QueryAllLayerButton()
        {
            base.m_caption = "全图检索";
            base.m_toolTip = base.m_caption;
        }

        /// <summary>
        /// 应用查询
        /// </summary>
        IApplication m_Application = null;

        /// <summary>
        /// 查询窗体
        /// </summary>
        private FrmQueryAllLayer m_frmQueryAll;

        /// <summary>
        /// 创建按钮
        /// </summary>
        /// <param name="hook"></param>
        public override void OnCreate(object hook)
        {
            if (m_Application == null)
            {
                m_Application = hook as IApplication;
            }
        }

        /// <summary>
        /// 点击事件
        /// </summary>
        public override void OnClick()
        {
            if (m_Application == null) return;
            if (m_frmQueryAll == null || m_frmQueryAll.IsDisposed)
            {
                m_frmQueryAll = new FrmQueryAllLayer();
                m_frmQueryAll.Application = this.m_Application;
            }
            m_frmQueryAll.Show((m_Application as IFramework).MdiForm);
        }

        /// <summary>
        /// 按钮图标
        /// </summary>
        public override int Bitmap
        {
            get
            {
                string bitmapResourceName = this.GetType().Namespace + ".Res." + this.GetType().Name + ".png";
                return new Bitmap(GetType().Assembly.GetManifestResourceStream(bitmapResourceName)).GetHbitmap().ToInt32();
            }
        }

        /// <summary>
        /// 是否可用
        /// </summary>
        public override bool Enabled
        {
            get
            {
                if (m_Application == null || !(m_Application as IFramework).IsMapCtrlactive) return false;
                if ((m_Application.Document as IMxDocument).ActiveView.FocusMap.LayerCount == 0) return false;
                IEnumLayer layers = (m_Application.Document as IMxDocument).ActiveView.FocusMap.get_Layers(null, true);
                ILayer layer = layers.Next();
                while (layer != null)
                {
                    if (layer.Valid && (layer is IFeatureLayer || layer is IBasemapSubLayer))
                    {
                        System.Runtime.InteropServices.Marshal.ReleaseComObject(layers);
                        return true;
                    }
                    layer = layers.Next();
                }
                System.Runtime.InteropServices.Marshal.ReleaseComObject(layers);
                return false;
            }
        }

    }
}